#include <QPainter>
#include <QVariant>
#include "ColorSelector.h"

ColorSelector::ColorSelector(QWidget *parent ) : QFrame( parent )
{
	initialize("",QColor(87,114,0));
}

ColorSelector::ColorSelector(string name, QColor color, QWidget *parent ) : QFrame( parent )
{
	initialize(name,color);
}

void ColorSelector::initialize(string name, QColor color)
{
	m_currentColor = color;
	m_name = name;
	this->setContentsMargins(1,3,3,3);
	this->setFrameStyle(QFrame::StyledPanel);
	QHBoxLayout * tophlayout = new QHBoxLayout;
	m_label = new QLabel(QString(name.c_str()),this);
	tophlayout->addWidget(m_label); // <<--

	m_colorWidget = new ColorWidget(this);
	m_colorWidget->setColor(color);
	m_colorWidget->setFixedSize(60,25);	
	tophlayout->addWidget(m_colorWidget); // <<--

	QHBoxLayout *hlayout = new QHBoxLayout;
	{
		hlayout->setSpacing(0);
		QVBoxLayout * barsLayout = new QVBoxLayout;
		{
			barsLayout->setSpacing(0);
			for(int i=0; i<3; i++)
			{
				char lineNames[3][2] ={"R","G","B"};
				QHBoxLayout * lineLayout = new QHBoxLayout;
				lineLayout->setSpacing(1);
				m_sliders[i] = new QSlider(Qt::Horizontal, this);
				m_sliders[i]->setRange(0,255);
				m_sliders[i]->setSingleStep(1);
				m_sliders[i]->setFixedHeight(25);
				QLabel * label = new QLabel(QString(lineNames[i]), this);
				label->setFixedHeight(25);
				label->setAlignment(Qt::AlignHCenter);
				lineLayout->addWidget(label);
				lineLayout->addWidget(m_sliders[i]);

				m_spinboxes[i] = new QSpinBox(this);
				m_spinboxes[i]->setRange(0,255);
				m_spinboxes[i]->setFixedHeight(25);

				lineLayout->addWidget(m_spinboxes[i]); // <<--
				QObject::connect(m_spinboxes[i], SIGNAL(valueChanged(int)), m_sliders[i], SLOT(setValue(int)));
				QObject::connect(m_sliders[i], SIGNAL(valueChanged(int)), m_spinboxes[i], SLOT(setValue(int)));
				const char * slot = NULL;
				switch(i)
				{
					case 0:
						slot = SLOT(setR(int));
						m_sliders[i]->setValue(color.red());
						break;
					case 1:
						m_sliders[i]->setValue(color.green());
						slot = SLOT(setG(int));
						break;
					case 2:
						m_sliders[i]->setValue(color.blue());
						slot = SLOT(setB(int));
				}
				QObject::connect(m_sliders[i], SIGNAL(valueChanged(int)), m_colorWidget, slot);
				lineLayout->setMargin(0);
				barsLayout->setMargin(0);
				barsLayout->addLayout(lineLayout); // <<--
			}
		}
		hlayout->setMargin(0);;
		hlayout->addLayout(barsLayout); // <<--
	}
	QVBoxLayout *vlayout = new QVBoxLayout;
	vlayout->setContentsMargins(3,3,3,0);
	vlayout->setSpacing(0);
	vlayout->addLayout(tophlayout);
	vlayout->addLayout(hlayout); // <<--
	this->setLayout(vlayout);
	QObject::connect(m_colorWidget, SIGNAL(colorChanged(QColor)), this, SLOT(colorValueChanged(QColor)));
}

void ColorSelector::setColor(QColor color)
{
	m_sliders[0]->setValue(color.red());
	m_sliders[1]->setValue(color.green());
	m_sliders[2]->setValue(color.blue());
	m_colorWidget->setColor(color);
	m_currentColor = color;
}

void ColorSelector::colorValueChanged(QColor color)
{
	emit colorChanged(m_name, color);
	m_currentColor = color;
}
